Open In Colab

Author: Ben Garrett Created Date: 12/28/2020

The following article is writen as a Jupyter Notebook. All code can be run and tweaked by clicking the 'Open in Colab' button.

Introduction

Time series data naturally lends itself to animations as a user can watch a measure change as the year progresses. Building these charts can be a bit tricky, though. The Matplotlib library of Python contains some useful tools to create and export animated charts as gifs or mp4s. In this article, I will show some of these features using my Strava activity data from 2020. More info on the data and how it was collected can be fouind in my other post Visualizing My Strava Data with Python and Tableau.

Imports

The data coves from a csv dowloaded from Strava where each row is an activity (hike, run, ride) that I logged in 2020. The attributes are relevant measures like elevation gain, speed, etc.

Clean The Data

The following code creates several cummulative sum columns for seeing annual progress of the metrics of interest. In this case, the metrics are elevation gain (feet), duration of activity (hours) and distance (miles).

Create Static Chart of Progress Over Time

Create Animated GIF of Progress Over Time

Tho following code builds the same chart, but displays a gif where each frame is the above chart at each time step. For the chart to animate very smoothly, you would need to pad values to ensure there is representation for each timestep in the dataset, however for simplicity I did not do this here. The basic steps are as follows:

Creating More Complex Animations

The above chart is great at showing the distribution of my activities over time. Additionally, you can see differences in my average speed when the distance and duration lines diverge (relative slow down of my activities) and converge (speed up of my activities). We can make this much more apparent by charting cummulative means as opposed to sums. This will explicitely show how my speed, distance per activity and duration per activity varied as the year progressed. The below code:

Conclusion

Success! In the second chart, we can see taht my cycling speed was slowest during the summer. This may seem odd, but that is the season I put away my road bike and pick up my mountain bike. You can also see that my biggest hikes, in terms of all three metrics, were toward the end of the hiking season. The only improvement that might make since is a total line that sumarizes the progress of all three activity types. However, it does not make a lot of sense to compare speed accross activity types.

The Matplotlib library is powerful and robust, and includes lots of advances features to create complex charts. Literally every detail is adjustable. It may take more time to build custom plots in Python over commercially available software, but when precision is needed or the data is not conveniently structured, Python is my preferred choice.